package com.meihua.baoleyi.fragment.sickness;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseFragment;
import com.meihua.baoleyi.bean.CurePlanBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.ui.CommonWebActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 婴儿治疗方案
 * Created by liming on 2017/10/19.
 */

public class TherapCureFragment extends BaseFragment {

    @Bind(R.id.iv_head)
    ImageView ivHead;
    //    @Bind(R.id.tv_action1)
//    TextView tvAction1;
//    @Bind(R.id.tv_action2)
//    TextView tvAction2;
//    @Bind(R.id.rv_func)
    RecyclerView mRecyclerView;

    private Context mcontext;
    private static TreeMap<String, String> mTreeMap;
    private List<CurePlanBean.DataBean.ShopContentBean> dataList = new ArrayList<>();
    private MyAdapter mAdapter;
    private CurePlanBean bean;

    public static TherapCureFragment newInstance(String json) {
        TherapCureFragment fragment = new TherapCureFragment();
        Bundle mBundle = new Bundle();
        mBundle.putString("json", json);
        fragment.setArguments(mBundle);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mcontext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.therap_fragment1, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    /**
     * 初始化数据
     */
    private void initView() {
        String json = getArguments().getString("json");
        bean = NetHelper.getBeanForJson(CurePlanBean.class, json);
        dataList.clear();
        dataList.addAll(bean.getData().getShop_content());
        CurePlanBean.DataBean dataBean = bean.getData();

        Glide.with(mContext).load(dataBean.getOriginal_img()).error(R.mipmap.chanpin).into(ivHead);

//        tvAction1.setText(dataBean.getGoods_content());

        //设置列表数据
        mAdapter = new MyAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setNestedScrollingEnabled(false);
    }

    /**
     * 点击进入详情
     */

    @OnClick(R.id.submit)
    public void onViewClicked() {
        Intent intent = new Intent(getActivity(), CommonWebActivity.class);
        intent.putExtra("url", APIServer.GOODSDETAIL + MyApp.getInstance().getToken() + "/" + MyApp.getInstance().getUser_id() + "/" + bean.getData().getGoods_id());
        startActivity(intent);
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        public MyAdapter() {
            super();
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyViewHolder) holder).displayData(dataList.get(position));
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_there_cure, parent, false);
            return new MyViewHolder(v);
        }

        @Override
        public int getItemCount() {
            return dataList.size() > 0 ? dataList.size() : 0;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView name;
            private TextView detailName;

            public MyViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.tv_name);
                detailName = itemView.findViewById(R.id.tv_detail_name);
            }

            public void displayData(CurePlanBean.DataBean.ShopContentBean data) {
                name.setText(data.getName());
                detailName.setText(data.getContent());
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(getActivity());
    }
}
